package top.oylan.manager.controller.mp;
import org.springframework.web.bind.annotation.*;
import top.oylan.manager.entity.Category;
import top.oylan.manager.entity.Course;
import top.oylan.manager.entity.Product;
import top.oylan.manager.entity.mp.MpResult;
import top.oylan.manager.service.CategoryService;
import top.oylan.manager.service.ProductService;
import top.oylan.manager.tools.Pager;
import top.oylan.manager.tools.SystemConstants;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @Author abduahad
 * @Description ${description}
 * @Date 2019-08-11
 */
@RestController
@CrossOrigin
@RequestMapping("/mp/course/")
public class MpCourseController {

    @Resource
    ProductService productService;

    @Resource
    CategoryService categoryService;

    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public MpResult index(@RequestParam(value = "page",defaultValue = "1")Integer page,
                          @RequestParam(value = "cateId",required = false)Short cateId){

        MpResult result = new MpResult();
        Pager pager = new Pager<>();
        pager.setCurrentPage(page);
        pager.setEveyPageRow(SystemConstants.PAGER_COURSE_ROW_SIZE);
        Product condition = new Product();
        condition.setType((byte)1);
        if(cateId != null && cateId > 0){
            List<Category> categories = categoryService.selectCategoryByType(1);
            List<Short> shorts = categoryService.categoryTree(categories, new ArrayList<>(Arrays.asList((short) cateId)), cateId);
            condition.setCateIdStr(shorts);
        }
        pager.setCondition(condition);
        List<Course> courses = productService.selectCourseByPager(pager);
        pager.setList(courses);
        result.setData(pager);
        return result;
    }

}
